﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BaseImageUseCase.cs" company="">
//   
// </copyright>
// <summary>
//   The base image use case.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.IO;

namespace Libium.UseCases
{
    using Libium.Core;
    using Libium.Drawing;

    /// <summary>
    ///   The base image use case.
    /// </summary>
    public abstract class BaseImageUseCase : BaseFileUseCase, IImageUseCase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseImageUseCase"/> class.
        /// </summary>
        /// <param name="parent">
        /// The parent. 
        /// </param>
        /// <param name="service">
        /// The service. 
        /// </param>
        protected BaseImageUseCase(ICompoundUseCase parent, IUseCaseInstanceService service)
            : base(parent, service)
        {
            MimeTypes = Core.MimeTypes.Images;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The build paste options.
        /// </summary>
        /// <param name="data">
        /// The data. 
        /// </param>
        /// <returns>
        /// The <see cref="PasteOptions"/> . 
        /// </returns>
        protected override PasteOptions BuildPasteOptions(ICopyData data)
        {
            var options = base.BuildPasteOptions(data);
            var can = data.IsImageExist(MimeTypes);
            options.CanCopy |= can;
            options.CanMove |= can;
            return options;
        }

        /// <summary>
        /// The do paste.
        /// </summary>
        /// <param name="data">
        /// The data. 
        /// </param>
        /// <param name="action">
        /// The action. 
        /// </param>
        protected override void DoPaste(ICopyData data, CopyAction action)
        {
            var image = data.GetImage(NotExistsAction.ReturnNull);
            if (image != null)
            {
                var file = image.GetFile();
                var copyFile = LocalFile.CreateTemporaryFile(file.GetDescription().Extension);
                file.CopyTo(copyFile);
                FilePath = copyFile.GetPath();
                return;
            }

            base.DoPaste(data, action);
        }

        /// <summary>
        ///   The do start copy.
        /// </summary>
        /// <returns> The <see cref="ICopyData" /> . </returns>
        protected override ICopyData DoStartCopy()
        {
            var data = base.DoStartCopy();
            data.SetImage(new NetImage(FilePath));
            return data;
        }

        protected override void OnFileChanged()
        {
            //TODO Remove temporary fix
            try
            {
                if (!string.IsNullOrEmpty(FilePath))
                {
                    var bitmap = new Bitmap(FilePath);
                    bitmap.Dispose();
                }
                base.OnFileChanged();
            }
            catch (ArgumentException)
            {
            }
            catch (InvalidOperationException)
            {
            }
            catch (FileNotFoundException)
            {
            }
        }

        #endregion
    }
}